package dao.hibernate;

import java.util.List;

import model.SysModule;

import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import dao.SysModuleDao;

/**
 * @see model.SysModule
 */
@Repository
public class SysModuleDaoHibernate extends GenericDaoHibernate<SysModule, Integer> implements SysModuleDao {
	public SysModuleDaoHibernate() {
		super(SysModule.class);
	}

	private static final Logger log = LoggerFactory
			.getLogger(SysModuleDaoHibernate.class);
	// property constants
	public static final String MODULE_NAME = "moduleName";
	public static final String MODULE_ACTION = "moduleAction";
	public static final String PARENT_MODULE_ID = "parentModuleId";
	public static final String MODULE_ORDER = "moduleOrder";
	public static final String OPEN_TARGET = "openTarget";
	public static final String IS_ENABLE = "isEnable";
	public static final String IS_DISPLAY = "isDisplay";

	protected void initDao() {
		// do nothing
	}

	public void delete(SysModule persistentInstance) {
		log.debug("deleting SysModule instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public SysModule findById(java.lang.Integer id) {
		log.debug("getting SysModule instance with id: " + id);
		try {
			SysModule instance = (SysModule) getHibernateTemplate().get(
					"model.SysModule", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<SysModule> findByExample(SysModule instance) {
		log.debug("finding SysModule instance by example");
		try {
			List<SysModule> results = (List<SysModule>) getHibernateTemplate()
					.findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding SysModule instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from SysModule as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<SysModule> findByModuleName(Object moduleName) {
		return findByProperty(MODULE_NAME, moduleName);
	}

	public List<SysModule> findByModuleAction(Object moduleAction) {
		return findByProperty(MODULE_ACTION, moduleAction);
	}

	public List<SysModule> findByParentModuleId(Object parentModuleId) {
		return findByProperty(PARENT_MODULE_ID, parentModuleId);
	}

	public List<SysModule> findByModuleOrder(Object moduleOrder) {
		return findByProperty(MODULE_ORDER, moduleOrder);
	}

	public List<SysModule> findByOpenTarget(Object openTarget) {
		return findByProperty(OPEN_TARGET, openTarget);
	}

	public List<SysModule> findByIsEnable(Object isEnable) {
		return findByProperty(IS_ENABLE, isEnable);
	}

	public List<SysModule> findByIsDisplay(Object isDisplay) {
		return findByProperty(IS_DISPLAY, isDisplay);
	}

	public List findAll() {
		log.debug("finding all SysModule instances");
		try {
			String queryString = "from SysModule s";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public SysModule merge(SysModule detachedInstance) {
		log.debug("merging SysModule instance");
		try {
			SysModule result = (SysModule) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(SysModule instance) {
		log.debug("attaching dirty SysModule instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(SysModule instance) {
		log.debug("attaching clean SysModule instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

}